package zrar;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import zrar.entity.Prize;
import zrar.util.SystemUtil;



public class SetPrizeFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	/** top **/
	private JPanel topPane;

	private JLabel prizename_lb; // 奖项 名称，一等奖，二等奖
	private JLabel prizenum_lb; // 奖品数量
	private JLabel content_lb; // 奖品内容，礼物
	private JLabel fetchnum_lb; // 抽奖的时候，一次性抽多少人

	private JTextField name_txt;
	private JTextField total_txt;
	private JTextField content_txt;
	private JTextField fetchnum_txt;

	private JButton add_but;
	private JButton reset_but;
	/** top **/

	/** middle **/
	private JPanel middlePane;
	private JLabel prize_lb;
	private JList list;
	private PrizeModelList mode;
	private JScrollPane scrollPane;

	private JButton up_but;
	private JButton down_but;
	private JButton del_but;
	
	private JLabel res_lb; //结果显示排序
	private JList res_list;
	private ResultModelList res_mode;
	private JScrollPane res_scrollPane;
	private JButton res_up_but;
	private JButton res_down_but;
	/** middle **/

	/** bottom **/
	private JPanel bottomPane;
	private JButton save_but;
	private JButton cancel_but;
	/** bottom **/

	static final int WIDTH = 480;
	static final int HEIGHT = 500;
	private int position_x = 30;
	private int position_y = 20;
	
	private int top_pane_x =5;
	private int top_pane_y =5;
	
	private int mid_pane_x =10;
	private int mid_pane_y =10;
	
	private List<Prize> sys_prizes = new ArrayList<Prize>();
	private List<Prize> local_prizes = new ArrayList<Prize>();
	private Prize prize;
	
	private List<String> res_local_prizes = new ArrayList<String>();
	private List<String> res_sys_prizes = new ArrayList<String>();

	public SetPrizeFrame() {
		this.setTitle("设置奖项");
		
		//顶部
		topPane = new JPanel();
		topPane.setLayout(null);

		prizename_lb = new JLabel("奖项名称");
		prizenum_lb = new JLabel("奖项数量");
		content_lb = new JLabel("奖品内容");
		fetchnum_lb = new JLabel("一次抽");

		name_txt = new JTextField(10);
		total_txt = new JTextField(5);
		content_txt = new JTextField(6);
		fetchnum_txt = new JTextField(5);

		add_but = new JButton("添加");
		reset_but = new JButton("重置");
		
		topPane.add(prizename_lb);topPane.add(prizenum_lb);
		topPane.add(content_lb);topPane.add(fetchnum_lb);
		
		topPane.add(name_txt);topPane.add(total_txt);
		topPane.add(content_txt);topPane.add(fetchnum_txt);
		
		topPane.add(add_but);topPane.add(reset_but);
		
		//中间部分
		middlePane = new JPanel();
		middlePane.setLayout(null);
		
		prize_lb = new JLabel("抽奖顺序");
		mode = new PrizeModelList();
		list = new JList(mode);
		scrollPane = new JScrollPane(list);		
		up_but = new JButton("向上");
		down_but = new JButton("向下");
		del_but = new JButton("删除");
		
		res_lb = new JLabel("结果显示顺序");
		res_mode = new ResultModelList();
		res_list = new JList(res_mode);
		res_scrollPane = new JScrollPane(res_list);
		res_up_but = new JButton("向上");
		res_down_but = new JButton("向下");
		
		middlePane.add(prize_lb);
		middlePane.add(scrollPane);
		middlePane.add(up_but);
		middlePane.add(down_but);
		middlePane.add(del_but);
		
		middlePane.add(res_scrollPane);
		middlePane.add(res_up_but);
		middlePane.add(res_down_but);
		middlePane.add(res_lb);
		
		//底部
		bottomPane = new JPanel();
		FlowLayout flow = new FlowLayout(FlowLayout.CENTER,60,5);
		bottomPane.setLayout(flow);
		save_but = new JButton("保存");
		cancel_but = new JButton("取消");
		
		bottomPane.add(save_but);
		bottomPane.add(cancel_but);

		init();
		this.add(topPane);
		this.add(middlePane);
		this.add(bottomPane);
		this.setSize(WIDTH, HEIGHT);
		SystemUtil.showInCenter(this);
		//this.setVisible(true);
	}
	
	public void hideFrame(){
		this.setVisible(false);
	}
	public void showFrame(){
		local_prizes.clear();
		sys_prizes=SystemUtil.getPrizelist();
		local_prizes.addAll(sys_prizes);
		
		mode = new PrizeModelList();
		list.setModel(mode);
		
		res_local_prizes.clear();
		res_sys_prizes=SystemUtil.getResultPrizes();
		res_local_prizes.addAll(res_sys_prizes);
		
		res_mode = new ResultModelList();
		res_list.setModel(res_mode);
		
		this.setVisible(true);
	}

	private void init() {
		this.setLayout(null);

		this.getContentPane().setBackground(Color.WHITE);

		topPane.setBounds(position_x, position_y, 400, 65);
		middlePane.setBounds(position_x, position_y + 90, 400, 265);
		bottomPane.setBounds(position_x, position_y + 370, 400, 40);
		
		prizename_lb.setBounds(top_pane_x, top_pane_y, 60, 25);
		name_txt.setBounds(top_pane_x+65, top_pane_y, 80, 25);		
		prizenum_lb.setBounds(top_pane_x+170, top_pane_y, 60, 25);
		total_txt.setBounds(top_pane_x+230, top_pane_y, 50, 25);
		add_but.setBounds(top_pane_x+310, top_pane_y, 60, 25);
		
		
		content_lb.setBounds(top_pane_x, top_pane_y+30, 60, 25);
		content_txt.setBounds(top_pane_x+65, top_pane_y+30, 80, 25);
		fetchnum_lb.setBounds(top_pane_x+170, top_pane_y+30, 60, 25);						
		fetchnum_txt.setBounds(top_pane_x+230, top_pane_y+30, 50, 25);				
		reset_but.setBounds(top_pane_x+310, top_pane_y+30, 60, 25);
		
		prize_lb.setBounds(mid_pane_x,mid_pane_y,80,25);
		scrollPane.setBounds(mid_pane_x,mid_pane_y+30,210,180);
		up_but.setBounds(mid_pane_x,mid_pane_y+220,60,25);
		down_but.setBounds(mid_pane_x+70,mid_pane_y+220,60,25);
		del_but.setBounds(mid_pane_x+140,mid_pane_y+220,60,25);
		
		res_lb.setBounds(mid_pane_x+235,mid_pane_y,180,25);
		res_scrollPane.setBounds(mid_pane_x+230,mid_pane_y+30,150,180);
		res_up_but.setBounds(mid_pane_x+250,mid_pane_y+220,60,25);
		res_down_but.setBounds(mid_pane_x+320,mid_pane_y+220,60,25);
		
		add_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 增加奖项
				AddPrize_Action(e);
			}
		});
		reset_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				ResetPrize_Action();
			}
		});
		up_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				Up_Action(e);
			}
		});
		down_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				Down_Action(e);
			}
		});
		del_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				DeletePrize_Action( e);
			}
		});
		
		res_up_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				ResultUp_Action(e);
			}
		});
		res_down_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				ResultDown_Action(e);
			}
		});
		
		save_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				Save_Action();
				
			}
		});
		cancel_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				hideFrame();
			}
		});
	}
	
	protected void Save_Action() {
		if(sys_prizes.size()!=0){
			sys_prizes.clear();
		}
		sys_prizes.addAll(local_prizes);
		SystemUtil.setPrizelist(sys_prizes);
		
		if(res_sys_prizes.size()!=0){
			res_sys_prizes.clear();
		}
		res_sys_prizes.addAll(res_local_prizes);
		SystemUtil.setResultPrizes(res_sys_prizes);
		
		hideFrame();
	}

	protected void ResultDown_Action(MouseEvent e) {
		
		int index = res_list.getSelectedIndex();
		
		if(index<(res_mode.size()-1)&&index>=0){
			String curPrize=res_local_prizes.get(index);
			String temp =res_local_prizes.get(index+1);
			
			res_local_prizes.set(index+1, curPrize);
			res_local_prizes.set(index, temp);
			
			res_mode = new ResultModelList();
			res_list.setModel(res_mode);
		}
	}

	protected void ResultUp_Action(MouseEvent e) {
		
		int index = res_list.getSelectedIndex();
		if(index>0){
			String temp =res_local_prizes.get(index-1);
			String curPrize=res_local_prizes.get(index);
			
			res_local_prizes.set(index-1, curPrize);
			res_local_prizes.set(index, temp);
			
			res_mode = new ResultModelList();
			res_list.setModel(res_mode);
		}
	}

	protected void Down_Action(MouseEvent e) {
		int index = list.getSelectedIndex();
		//System.out.println("当前选中的位置："+index);
		
		if(index<(mode.size()-1)&&index>=0){
			Prize curPrize=local_prizes.get(index);
			Prize temp =local_prizes.get(index+1);
			
			local_prizes.set(index+1, curPrize);
			local_prizes.set(index, temp);
			
			mode = new PrizeModelList();
			list.setModel(mode);
		}
	}

	protected void Up_Action(MouseEvent e) {
		int index = list.getSelectedIndex();
		//System.out.println("当前选中的位置："+index);
		if(index>0){
			Prize temp =local_prizes.get(index-1);
			Prize curPrize=local_prizes.get(index);
			
			local_prizes.set(index-1, curPrize);
			local_prizes.set(index, temp);
			
			mode = new PrizeModelList();
			list.setModel(mode);
		}		
	}

	protected void DeletePrize_Action(MouseEvent e) {
		List<String> deletePriz=new ArrayList<String>();
		
		int[] index= list.getSelectedIndices();
		int endindex=index.length-1;
		for(int i=endindex;i>=0;i--){
			//System.out.println("选中的===="+index[i]);
			mode.removeElementAt(index[i]);
			Prize prizz=local_prizes.remove(index[i]);
			deletePriz.add(prizz.getPname());
		}
		
		for(String pname :deletePriz){
			res_mode.removeElement(pname);
			res_local_prizes.remove(pname);
		}
		
		mode = new PrizeModelList();
		list.setModel(mode);
		
		res_mode = new ResultModelList();
		res_list.setModel(res_mode);
	}
	
	protected void ResetPrize_Action(){
		// 清除显示的
		name_txt.setText("");
		total_txt.setText("");
		content_txt.setText("");
		fetchnum_txt.setText("");
	}

	protected void AddPrize_Action(MouseEvent e) {
		String pname = name_txt.getText();
		String ptotal = total_txt.getText();
		String pcontent = content_txt.getText();
		String fetchnum = fetchnum_txt.getText();

		if(!checkInputContent( pname, ptotal, pcontent, fetchnum)){
			return;
		}
		
		// 保存 数据
		// System.out.println("pname="+pname+", ptotal="+ptotal+", pcontent="+pcontent);
		prize = new Prize();
		prize.setPname(pname);
		prize.setPcontent(pcontent);
		prize.setTotal(Integer.parseInt(ptotal));
		prize.setFetchnum(Integer.parseInt(fetchnum));
		prize.setIsFinish(false);
		local_prizes.add(prize);

		// 清除显示的
		ResetPrize_Action();
		mode = new PrizeModelList();
		list.setModel(mode);
		
		res_local_prizes.add(pname);
		res_mode = new ResultModelList();
		res_list.setModel(res_mode);		
	}

	public static void main(String[] args) {
		new SetPrizeFrame().showFrame();
	}
	
	private boolean checkInputContent(String pname,String ptotal,String pcontent,String fetchnum){
		if (pname == null || pname.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "奖项名称不能为空");
			return false;
		}
		if (pcontent == null || pcontent.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "奖品名称不能为空");
			return false;
		}
		if (ptotal == null || ptotal.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "奖项数量不能为空");
			return false;
		}
		if (!SystemUtil.isInteger(ptotal)) {
			JOptionPane.showMessageDialog(null, "奖项数量不是整数");
			return false;
		}
		if(Integer.parseInt(ptotal)==0){
			JOptionPane.showMessageDialog(null, "奖项数量应大于0");
			return false;
		}
		if (fetchnum == null || fetchnum.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "一次抽多少人数,不能为空");
			return false;
		}
		if (!SystemUtil.isInteger(fetchnum)) {
			JOptionPane.showMessageDialog(null, "一次抽奖项人数,不是整数");
			return false;
		}
		
		if(Integer.parseInt(fetchnum)>Integer.parseInt(ptotal)){
			JOptionPane.showMessageDialog(null, "一次抽奖的人数 比 总的奖项数量 还多");
			return false;
		}
		if(Integer.parseInt(fetchnum)==0){
			JOptionPane.showMessageDialog(null, "一次抽奖的人数 应大于0");
			return false;
		}
		
		return true;
	}
	
	class PrizeModelList extends DefaultListModel{
		private static final long serialVersionUID = 1L;		
		PrizeModelList() {

			for (int i = 0; i < local_prizes.size(); i++) {
				Prize priz = local_prizes.get(i);
				addElement((i + 1) + "." + priz.getPname() + "  共:"
						+ priz.getTotal()+"个,  一次抽:"+priz.getFetchnum()+"人");
			}
		}
	}
	
	class ResultModelList extends DefaultListModel{
		private static final long serialVersionUID = 1L;		
		ResultModelList() {

			for (int i = 0; i < res_local_prizes.size(); i++) {
				String res_prize_name = res_local_prizes.get(i);
				addElement((i + 1) + "." + res_prize_name);
			}
		}
	}
}
